home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / KISS.C < prev    next >
Text File  |  1993-08-09  |  4KB  |  171 lines

  1. /* Routines for AX.25 encapsulation in KISS TNC
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "config.h"
  6. #include "mbuf.h"
  7. #include "iface.h"
  8. #include "kiss.h"
  9. #include "slip.h"
  10. #include "asy.h"
  11. #include "ax25.h"
  12. #include "devparam.h"
  13.  
  14. /* Process incoming KISS TNC frame */
  15. void
  16. kiss_recv(struct iface *iface,struct mbuf *bp)
  17. {
  18.     struct iface *kissif;
  19.  
  20.     char kisstype = PULLCHAR(&bp);
  21.     int port = kisstype >> 4;
  22.  
  23.     if((kissif = Slip[iface->xdev].kiss[port]) == NULLIF) {
  24.         free_p(bp);
  25.         return;
  26.     }
  27.     if((kisstype & 0xf) == PARAM_DATA) {
  28.         ax_recv(kissif,bp);
  29.         return;
  30.     }
  31.     free_p(bp);
  32. }
  33.  
  34.  
  35. #if defined (KISS) && (defined(ASY) || defined(SCC))
  36. /* Send raw data packet on KISS TNC */
  37. int
  38. kiss_raw(struct iface *iface,struct mbuf *data)
  39. {
  40.     /* Put type field for KISS TNC on front */
  41.     struct mbuf *bp = pushdown(data,1);
  42.  
  43.     bp->data[0] = (iface->port << 4) | PARAM_DATA;
  44.  
  45.     if(iface->port) {
  46.         iface->rawsndcnt++;
  47.         iface->lastsent = secclock();
  48.     }
  49.     /* slip_raw also increments sndrawcnt */
  50.     slip_raw(Slip[iface->xdev].iface,bp);
  51.     return 0;
  52. }
  53.  
  54. /* Perform device control on KISS TNC by sending control messages */
  55. int32
  56. kiss_ioctl(struct iface *iface,int cmd,int set,int32 val)
  57. {
  58.     struct mbuf *hbp;
  59.     char *cp;
  60.  
  61.     /* At present, only certain parameters are supported by
  62.      * stock KISS TNCs. As additional params are implemented,
  63.      * this will have to be edited
  64.      */
  65.     switch(cmd){
  66.     case PARAM_RETURN:
  67.     case PARAM_RETURN2:
  68.         set = 1;    /* Note fall-thru */
  69.     case PARAM_TXDELAY:
  70.     case PARAM_PERSIST:
  71.     case PARAM_SLOTTIME:
  72.     case PARAM_TXTAIL:
  73.     case PARAM_FULLDUP:
  74.     case PARAM_HW:
  75.         if(!set) {
  76.             /* Can't read back */
  77.             return -1;
  78.         }
  79.         /* Allocate space for cmd and arg */
  80.         hbp = alloc_mbuf(2);
  81.         cp = hbp->data;
  82.         *cp++ = cmd;
  83.         *cp = val;
  84.         hbp->cnt = 2;
  85.  
  86.         if((int)hbp->data[0] != PARAM_RETURN) {
  87.             hbp->data[0] |= (iface->port << 4);
  88.         }
  89.         if(iface->port) {
  90.             iface->rawsndcnt++;
  91.             iface->lastsent = secclock();
  92.         }
  93.         /* Even more "raw" than kiss_raw */
  94.         slip_raw(Slip[iface->xdev].iface,hbp);
  95.         return (val * 10);
  96.     case PARAM_SPEED:    /* These go to the local asy driver */
  97.     case PARAM_DTR:
  98.     case PARAM_RTS:
  99.     case PARAM_UP:
  100.     case PARAM_DOWN:
  101.         return asy_ioctl(iface,cmd,set,val);
  102.     default:        /* Not implemented */
  103.         return -1;
  104.     }
  105. }
  106.  
  107. static int
  108. kiss_stop(struct iface *iface,int tmp)
  109. {
  110.     Slip[iface->xdev].kiss[iface->port] = NULLIF;
  111.     return 0;
  112. }
  113.  
  114. /* Attach a kiss interface to an existing asy interface in the system
  115.  * argv[0]: hardware type, must be "kiss"
  116.  * argv[1]: master interface, e.g., "ax4"
  117.  * argv[2]: kiss port, e.g., "4"
  118.  * argv[3]: interface label, e.g., "ax0"
  119.  * argv[4]: maximum transmission unit, bytes
  120.  */
  121. int
  122. kiss_attach(int argc,char **argv,void *p)
  123. {
  124.     struct iface *if_asy, *if_kiss;
  125.     int port;
  126.     static char portmsg[] = "Port %d already allocated on iface %s\n";
  127.  
  128.     if((if_asy = if_lookup(argv[1])) == NULLIF) {
  129.         tprintf(Badif,argv[1]);
  130.         return -1;
  131.     }
  132.     if(if_lookup(argv[3]) != NULLIF) {
  133.         tprintf(Ifexist,argv[4]);
  134.         return -1;
  135.     }
  136.     if((port = atoi(argv[2])) < 1) {
  137.         tprintf(portmsg,0,argv[1]);
  138.         return -1;
  139.     }
  140.     if(port > 15) {
  141.         port = 15;
  142.     }
  143.     if(Slip[if_asy->xdev].kiss[port] != NULLIF) {
  144.         tprintf(portmsg,port,argv[1]);
  145.         return -1;
  146.     }
  147.     /* Create interface structure and fill in details */
  148.     if_kiss = mxallocw(sizeof(struct iface));
  149.     if_kiss->addr = if_asy->addr;
  150.     if_kiss->name = strxdup(argv[3]);
  151.  
  152.     if_kiss->mtu = (argc >= 5) ? atoi(argv[4]) : if_asy->mtu;
  153.  
  154.     if_kiss->dev = if_asy->dev;
  155.     if_kiss->stop = kiss_stop;
  156.     setencap(if_kiss,"AX25");
  157.     if_kiss->ioctl = kiss_ioctl;
  158.     if_kiss->raw = kiss_raw;
  159.     if_kiss->hwaddr = strxdup(Mycall);
  160.     if_kiss->xdev = if_asy->xdev;
  161.     init_maxheard(if_kiss);
  162.     init_flags(if_kiss);
  163.     if_kiss->next = Ifaces;
  164.     if_kiss->niface = Niface++;
  165.     if_kiss->port = port;
  166.     Ifaces = if_kiss;
  167.     Slip[if_kiss->xdev].kiss[if_kiss->port] = if_kiss;
  168.     return 0;
  169. }
  170.  
  171. #endif /* ax25 + asy | scc */